<?php
declare(strict_types=1);

namespace App\Controller\Backend;

use App\Logic\AdminLiveLogic;
use App\Service\LiveService;
use Exception;
use Hyperf\HttpServer\Annotation\AutoController;
use Psr\Http\Message\ResponseInterface;

#[AutoController(prefix: '/live', server: 'backend')]
class LiveController extends AbstractController
{
    /**
     * 直播列表
     * @return ResponseInterface
     */
    public function lists(): ResponseInterface
    {
        $input = $this->request->all();
        return $this->apiReturn(200, 'succ', AdminLiveLogic::listLive($input));
    }

    /**
     * 直播间详情
     * @return ResponseInterface
     */
    public function detail(): ResponseInterface
    {
        $roomId = (int)$this->request->input('roomId', 0);
        if (!$roomId) {
            return $this->apiReturn(400, '参数错误');
        }
        $data = AdminLiveLogic::detailLive($roomId);
        return $this->apiReturn(200, 'succ', $data);
    }

    /**
     * 修改直播间信息
     * @return ResponseInterface
     */
    public function edit(): ResponseInterface
    {
        $roomId = (int)$this->request->input('roomId', 0);
        if (!$roomId) {
            return $this->apiReturn(400, '参数错误');
        }
        $input = $this->request->all();
        try {
            AdminLiveLogic::editLive($roomId, $input);
            return $this->apiReturn(200, '修改成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 直播断流
     * @return ResponseInterface
     */
    public function cutOffStream(): ResponseInterface
    {
        $roomId = (int)$this->request->input('roomId', 0);
        if (!$roomId) {
            return $this->apiReturn(400, '参数错误');
        }
        $ret = LiveService::dropLiveStream($roomId);
        if ($ret === true) {
            return $this->apiReturn(200, '操作成功');
        }
        return $this->apiReturn(400, '操作失败:' . $ret);
    }

    /**
     * 直播礼物记录
     * @return ResponseInterface
     */
    public function giftRecords(): ResponseInterface
    {
        $input = $this->request->all();
        return $this->apiReturn(200, 'succ', AdminLiveLogic::giftRecords($input));
    }

    /**
     * 聊天记录
     * @return ResponseInterface
     */
    public function chatRecords(): ResponseInterface
    {
        $input = $this->request->all();
        return $this->apiReturn(200, 'succ', AdminLiveLogic::chatRecords($input));
    }

    /**
     * 栏目列表
     * @return ResponseInterface
     */
    public function column(): ResponseInterface
    {
        $input = $this->request->all();
        return $this->apiReturn(200, 'succ', AdminLiveLogic::listColumn($input));
    }

    /**
     * 添加栏目
     * @return ResponseInterface
     */
    public function addColumn(): ResponseInterface
    {
        $input = $this->request->all();
        try {
            AdminLiveLogic::addColumn($input);
            return $this->apiReturn(200, '添加成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 修改栏目
     * @return ResponseInterface
     */
    public function editColumn(): ResponseInterface
    {
        $id = (int)$this->request->input('id', 0);
        if (!$id) {
            return $this->apiReturn(400, '参数错误');
        }
        $input = $this->request->all();
        if (empty($input['title'])) {
            return $this->apiReturn(400, '栏目名称不能为空');
        }
        try {
            AdminLiveLogic::editColumn($id, $input);
            return $this->apiReturn(200, '修改成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 删除栏目
     * @return ResponseInterface
     */
    public function delColumn(): ResponseInterface
    {
        $id = (int)$this->request->input('id');
        if (!$id) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            AdminLiveLogic::delColumn($id);
            return $this->apiReturn(200, '删除成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }

    /**
     * 快速修改栏目
     * @return ResponseInterface
     */
    public function updateColumnField(): ResponseInterface
    {
        $id = (int)$this->request->input('id');
        $field = $this->request->input('field', '');
        $value = (int)$this->request->input('value', 0);
        if (!$id || empty($field)) {
            return $this->apiReturn(400, '参数错误');
        }
        try {
            AdminLiveLogic::updateColumnField($id, $field, $value);
            return $this->apiReturn(200, '修改成功');
        } catch (Exception $e) {
            return $this->apiReturn(400, $e->getMessage());
        }
    }
}